﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;


public partial class Private_User : System.Web.UI.Page
{

    String StrConn = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    UsersHandler objUsersHandler = new UsersHandler();
    string UserIDUpdate;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            this.LoadGridView();
        }
    }

    protected void LoadGridView()
    {
        List<Users> lstUser = objUsersHandler.GetAllUsers(StrConn) ;
        GridViewUsers.DataSource = lstUser;
        GridViewUsers.DataBind();

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        if (txtPasswords.Text.Equals(txtRePass.Text))
        {
            Users objUsers = new Users();

            objUsers.Created = DateTime.Now.ToString();
            objUsers.Updated = DateTime.Now.ToString();
            objUsers.Isactive = "1";
            objUsers.Descriptions = txtDescriptions.Text.ToString();
            objUsers.UserName = txtUserName.Text.ToString();
            objUsers.Password = txtPasswords.Text.ToString();
            objUsers.Email = txtEmail.Text.ToString();
            objUsers.Phone = txtPhone.Text.ToString();

            objUsers.Level = ddlLevel.SelectedValue.ToString();

            if (ckbBlock.Checked == true)
            {

                objUsers.Block = "1";
            }
            else
            {
                objUsers.Block = "0";
            }
            objUsers.FullName = txtFullName.Text.ToString();
            objUsers.Address = txtAddress.Text.ToString();
            objUsers.IdentityCard = txtIdentitycard.Text.ToString();

            objUsersHandler.AddNewUsers(objUsers, StrConn);
            this.LoadGridView();
            this.setText();
        }
        else
        {
            lblErrorPass.Text = "Your password is not match. Retry?";
        }
    }

    protected void setText()
    {
        this.txtEmail.Text = "";
        this.txtFullName.Text = "";
        this.txtIdentitycard.Text = "";
        this.txtPasswords.Text = "";
        this.txtPhone.Text = "";
        this.txtUserName.Text = "";
        this.txtAddress.Text = "";
        this.txtDescriptions.Text = "";
        this.ckbBlock.Checked = true;
        lblErrorPass.Text = "";
    }

    protected void BindTextBox(int key)
    {

        string userID = GridViewUsers.DataKeys[key].Value.ToString();

        Users objUsers = new Users();

        objUsers = objUsersHandler.GetUsersDetailsForPrivate(userID, StrConn);

        txtUserName.Text = objUsers.UserName;
        txtPasswords.Text = objUsers.Password;
        txtRePass.Text = objUsers.Password;
        txtEmail.Text = objUsers.Email;
        txtPhone.Text = objUsers.Phone;
        ddlLevel.SelectedValue = objUsers.Level;

        if (objUsers.Block.Equals("0"))
        {
            this.ckbBlock.Checked = false;
        }
        else {
            this.ckbBlock.Checked = true;
        }

        txtFullName.Text = objUsers.FullName;
        txtAddress.Text = objUsers.Address;
        txtIdentitycard.Text = objUsers.IdentityCard;
        txtDescriptions.Text = objUsers.Descriptions;

    }

    protected void GridViewUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName != String.Empty)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            UserIDUpdate = GridViewUsers.DataKeys[index].Value.ToString();
            Session["LocationIUpdate"] = UserIDUpdate;
            switch (e.CommandName.ToString())
            {
                case "View":
                    BindTextBox(index);
                    break;

            }
        }
    }
    protected void btnClearText_Click(object sender, EventArgs e)
    {
        this.setText();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        

        Users objUsers = new Users();
        string idUpdate = Session["LocationIUpdate"].ToString();
        objUsers = objUsersHandler.GetUsersDetailsForPrivate(idUpdate, StrConn);

        objUsers.UserName = txtUserName.Text;
        objUsers.Updated = DateTime.Now.ToString();
        objUsers.Isactive = "1";
        objUsers.Descriptions = txtDescriptions.Text.ToString();
       
        //objUsers.Password = txtPasswords.Text.ToString();
        //objUsers.Email = txtEmail.Text.ToString();
        objUsers.Phone = txtPhone.Text.ToString();
        objUsers.Level = ddlLevel.SelectedValue.ToString();

        if (ckbBlock.Checked == true)
        {

            objUsers.Block = "1";
        }
        else
        {
            objUsers.Block = "0";
        }
        objUsers.FullName = txtFullName.Text.ToString();
        objUsers.Address = txtAddress.Text.ToString();
        objUsers.IdentityCard = txtIdentitycard.Text.ToString();

        objUsersHandler.UpdateUsers(objUsers, StrConn);
        this.LoadGridView();
        this.setText();

    }
}